package com.finalx.timersms;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.os.PowerManager;
import android.os.PowerManager.WakeLock;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;

public class VibratorActivity extends Activity{
	final String TAG = "VibratorActivity";
	
	static final long VIBRATE_TOTAL_TIME = 1000;
	
	SeekBar mSpeedSeekBar = null;
	Button mStartStopButton = null;
	

	WakeLock mWakeLock = null;

	int	  mSelectedSpeed = 0;
	
	private IVibrateRate vibrateService;
	private ServiceConnection serviceConnection = new ServiceConnection() {

		public void onServiceConnected(ComponentName name, IBinder service) {
			vibrateService = (IVibrateRate) service;
			Log.d(TAG, " on serivce connected");
		}

		public void onServiceDisconnected(ComponentName name) {
			vibrateService = null;
		}
	};
	

	
    private OnSeekBarChangeListener seekBarListener=new OnSeekBarChangeListener(){
    	 
		public void onProgressChanged(SeekBar seekBar, int progress,
				boolean fromUser) {
			Log.d(TAG, "speedbar changed to:" + progress);
			mSelectedSpeed = progress;
			int maxSpeed = seekBar.getMax();
			long vibrateKeepInMs = VIBRATE_TOTAL_TIME / maxSpeed * progress;
			
			vibrateService.off();
			vibrateService.set(vibrateKeepInMs, (VIBRATE_TOTAL_TIME - vibrateKeepInMs));
			vibrateService.on();
		}

		public void onStartTrackingTouch(SeekBar seekBar) {
			// TODO Auto-generated method stub
			
		}

		public void onStopTrackingTouch(SeekBar seekBar) {
			// TODO Auto-generated method stub
			
		}
    };

	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		
		//get widgets from layout ++++
		setContentView(R.layout.vibrator_layout);
		mStartStopButton = (Button) findViewById(R.id.button_start_stop);
		mSpeedSeekBar = (SeekBar) findViewById(R.id.speedSeekBar);
		//get widgets from layout ----
		
		mSpeedSeekBar.setProgress(mSpeedSeekBar.getMax()/2);
		mSpeedSeekBar.setOnSeekBarChangeListener(seekBarListener);
		
		mWakeLock = ((PowerManager) getSystemService(Context.POWER_SERVICE)).newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, TAG);
		openWakeLock(mWakeLock);
		
		this.bindService(new Intent("com.finalx.timersms.VibratorService"), this.serviceConnection, BIND_AUTO_CREATE);
		
	}
    
    @Override
	protected void onStop() {
		// TODO Auto-generated method stub
		super.onStop();
		Log.d(TAG, "onStop() called ++--");
		vibrateService.off();
		closeWakeLock(mWakeLock);
	}
    
    public void onDestroy(){
    	this.unbindService(serviceConnection);
    	super.onDestroy();
    }

    public void onStartStopButtonClicked(View v) {
    	Log.d(TAG, "onStartStopButtonClicked, vibrating:" + vibrateService.isOn() + ", speed:" + mSpeedSeekBar.getProgress());
    	if(!vibrateService.isOn()) {
    		vibrateService.on();
    	} else {
    		vibrateService.off();
    	}
    }

    
    public void openWakeLock(WakeLock wlock) {
    	if(wlock.isHeld()) {
    		return;
    	}
    	Log.d(TAG, "WakeLock acquired ++++++++");
    	wlock.acquire();
    }
    
    public void closeWakeLock(WakeLock wlock) {
    	if(!wlock.isHeld()) {
    		return;
    	}
    	Log.d(TAG, "WakeLock released --------");
    	wlock.release();
    }
}
